<?php
#+------------------------------------------------------------------
#| 普通的。
#+------------------------------------------------------------------
#| Author:Janmas Cromwell <janmas-cromwell@outlook.com>
#+------------------------------------------------------------------
namespace app\library\redis;

use Swoole\Database\RedisConfig;
use Swoole\Database\RedisPool;

/**
 * @property RedisPool $pool
 */
class Redis
{

    protected static $pool;

    /**
     * @return \Redis
     */
    static function get()
    {
        self::buildPool();
        return self::$pool->get();
    }

    private static function buildPool()
    {
        if (is_null(self::$pool)) {
            $config = config('redis');
            $redisConfig = new RedisConfig();
            $redisConfig->withHost($config['host'])->withPort($config['port']);

            if (isset($config['select']) && $config['select'] >= 0) {
                $redisConfig->withDbIndex($config['select']);
            }

            if (isset($config['auth']) && !empty($config['auth'])) {
                $redisConfig->withAuth($config['auth']);
            }
            if (isset($config['timeout']) && $config['timeout'] > 0) {
                $redisConfig->withTimeout($config['timeout']);
            }
            self::$pool = new RedisPool($redisConfig);
        }

    }

    static function put($instance)
    {
        self::buildPool();
        self::$pool->put($instance);
        return true;
    }
}
